
# --- local modules can be added here --- #

LOCAL_GRAPHICS    	:=
LOCAL_PLANTS      	:= 	
LOCAL_CONTROLLERS 	:= 
# --------------------------------------- #


# --- standard modules are defined here --- #

GRAPHICS          	:= 	Maze
PLANTS 			:= 	Maze
CONTROLLERS 		:=	DemoControl Amaze Explore Optimist Tuqute

# --------------------------------------- #

# --- define libs to compile not covered by rules here ------- #
# The name has to fit the directory name. 
INTLIBS			:= n++ Algebra 
#STDLIBS			:= -lpcan -pthread
STDLIBS			:=  -pthread
# --------------------------------------- #

# --- some dependecies----------------------- #
# module                   : intlib   |   stdlib
# ........................................... #
# plant CartPoleNI_Real    : TmcPendelCtr | -lpcan -lpthread
#
# ------------------------------------------- #

# --------------------------------------- #
# Normally you don't have to change
# anything below this line.
# --------------------------------------- #

# --------------------------------

CLSQUARE_SRC		:= $(PWD)
KERNEL_DIR		:= $(CLSQUARE_SRC)/kernel
UTILS_DIR		:= $(CLSQUARE_SRC)/utils
LOCAL_UTILS_DIR		:= $(CLSQUARE_SRC)/local/utils
PLANT_DIR		:= $(CLSQUARE_SRC)/plant
LOCAL_PLANT_DIR	        := $(CLSQUARE_SRC)/local/plant
GRAPHIC_DIR		:= $(CLSQUARE_SRC)/graphic
LOCAL_GRAPHIC_DIR	:= $(CLSQUARE_SRC)/local/graphic
CONTROL_DIR		:= $(CLSQUARE_SRC)/control
LOCAL_CONTROL_DIR	:= $(CLSQUARE_SRC)/local/control

INTLIBS_DIR		:= $(CLSQUARE_SRC)/libs

# Add additional libs here. The name has to fit the directory name.
INTLIBS_TARGETS		:= $(foreach library, $(INTLIBS), $(CLSQUARE_SRC)/libs/$(library)/lib/lib$(library).a)
INTLIBS_INCLUDE		:= $(foreach library, $(INTLIBS), -I $(CLSQUARE_SRC)/libs/$(library)/include)
LIB_PATH		:= $(foreach library, $(INTLIBS), -L$(CLSQUARE_SRC)/libs/$(library)/lib/ )
LIBS			:= $(foreach library, $(INTLIBS), -l$(library))

LIBS			+= $(STDLIBS)

CC 			:= g++
LC 			:= g++

DEST_DIR 		:= ../bin
TARGET   		:= clsquare

SUFFIXES 		:= .c .cc .cpp

INCLUDE_PATH := -I$(KERNEL_DIR) \
             $(patsubst %, -I $(PLANT_DIR)/%, $(PLANTS))        \
             $(patsubst %, -I $(LOCAL_PLANT_DIR)/%, $(LOCAL_PLANTS))        \
             $(patsubst %, -I $(CONTROL_DIR)/%, $(CONTROLLERS)) \
             $(patsubst %, -I $(LOCAL_CONTROL_DIR)/%, $(LOCAL_CONTROLLERS)) \
             $(patsubst %, -I $(GRAPHIC_DIR)/%, $(GRAPHICS))    \
             $(patsubst %, -I $(LOCAL_GRAPHIC_DIR)/%, $(LOCAL_GRAPHICS))    \
             -I$(UTILS_DIR) -I$(LOCAL_UTILS_DIR) -I$(GRAPHIC_DIR) -I$(LOCAL_GRAPHIC_DIR) \
             $(INTLIBS_INCLUDE)

CFLAGS			:= -Wall -Wno-deprecated -DCLSQUARE -O2

SRCS := $(shell ./sources.sh $(KERNEL_DIR))  $(shell ./sources.sh $(UTILS_DIR))  $(shell ./sources.sh $(LOCAL_UTILS_DIR)) $(foreach graphic, $(GRAPHICS), $(shell ./sources.sh $(GRAPHIC_DIR)/$(graphic))) $(foreach graphic, $(LOCAL_GRAPHICS), $(shell ./sources.sh $(LOCAL_GRAPHIC_DIR)/$(graphic))) $(foreach plant, $(PLANTS), $(shell ./sources.sh $(PLANT_DIR)/$(plant))) $(foreach plant, $(LOCAL_PLANTS), $(shell ./sources.sh $(LOCAL_PLANT_DIR)/$(plant))) $(foreach controller, $(CONTROLLERS), $(shell ./sources.sh $(CONTROL_DIR)/$(controller))) $(foreach controller, $(LOCAL_CONTROLLERS), $(shell ./sources.sh $(LOCAL_CONTROL_DIR)/$(controller)))
OBJS := $(filter %.o,$(foreach suf,$(SUFFIXES),$(SRCS:$(suf)=.o)))
DEPS := $(filter %.d,$(foreach suf,$(SUFFIXES),$(SRCS:$(suf)=.d)))

export INCLUDE_PATH

seperator := echo; echo; echo "------------------------------------------";

default: $(TARGET)	
	@ln -sf ../src/clsquare ../bin/clsquare
	@(echo ">>>>>>>>>>> Finished <<<<<<<<<<<<<")

$(TARGET): $(INTLIBS_TARGETS) $(OBJS) $(DEPS)
	@$(seperator) echo "linking $@"
	$(LC) $(CFLAGS) $(LIB_PATH) -o $(TARGET) $(OBJS) $(LIBS)

$(INTLIBS_TARGETS):
	@$(seperator) echo "Building internal libs in ./libs/ "
	@$(patsubst %, $(seperator) echo "Compiling lib:  $@" ; cd $(INTLIBS_DIR)/%/src; $(MAKE);, $(INTLIBS))
	@echo "Finished building libs. " $(seperator)

depend: $(DEPS)
	@echo "subtarget dependecies generated."

%.d : %.cc $(INTLIBS_TARGETS)
	@echo "making dependencies $@"
	@$(CC) -MM -MT "$@ $(@:.d=.o)" $(CFLAGS) $(INCLUDE_PATH) -MF $@ $<

%.d : %.cpp $(INTLIBS_TARGETS)
	@echo "making dependencies $@"
	@$(CC) -MM -MT "$@ $(@:.d=.o)" $(CFLAGS) $(INCLUDE_PATH) -MF $@ $<

%.d : %.c $(INTLIBS_TARGETS)
	@echo "making dependencies $@"
	@$(CC) -MM -MT "$@ $(@:.d=.o)" $(CFLAGS) $(INCLUDE_PATH) -MF $@ $<

%.o : %.cc
	@echo "compiling $<"
	@$(CC) $(CFLAGS) $(INCLUDE_PATH) -c -o $@ $<

%.o : %.c
	@echo "compiling $<"
	@$(CC) $(CFLAGS) $(INCLUDE_PATH) -c -o $@ $<

%.o : %.cpp
	@echo "compiling $<"
	@$(CC) $(CFLAGS) $(INCLUDE_PATH) -c -o $@ $<

lists: 
	@echo $(shell ./sources.sh $(KERNEL_DIR) 1)
	@echo $(shell ./sources.sh $(UTILS_DIR) 1)
	@echo $(shell ./sources.sh $(LOCAL_UTILS_DIR) 1)
	@echo $(foreach graphic, $(GRAPHICS), $(shell ./sources.sh $(GRAPHIC_DIR)/$(graphic) 1))
	@echo $(foreach graphic, $(LOCAL_GRAPHICS), $(shell ./sources.sh $(LOCAL_GRAPHIC_DIR)/$(graphic) 1))
	@echo $(foreach plant, $(PLANTS), $(shell ./sources.sh $(PLANT_DIR)/$(plant) 1))
	@echo $(foreach plant, $(LOCAL_PLANTS), $(shell ./sources.sh $(LOCAL_PLANT_DIR)/$(plant) 1))
	@echo $(foreach controller, $(CONTROLLERS), $(shell ./sources.sh $(CONTROL_DIR)/$(controller) 1))
	@echo $(foreach controller, $(LOCAL_CONTROLLERS), $(shell ./sources.sh $(LOCAL_CONTROL_DIR)/$(controller) 1))

clean: lists
	@echo "removing $(TARGET), .o-files, .d-files"
	@rm -f $(OBJS) $(TARGET) $(DEPS)

clean.libs:
	@$(seperator) echo "Cleaning internal libs in ./libs/ "
	@$(patsubst %, $(seperator) echo "Cleaning lib: " ; cd $(INTLIBS_DIR)/%/src; $(MAKE) clean;, $(INTLIBS))
	@echo "Finished cleaning libs. " 
	@$(seperator)

clean.d:
	@echo "removing .d-files"
	@rm -f $(DEPS)

veryclean: clean clean.d clean.libs
	@echo "cleaning all objekts, dpendecies and libs ... done."

showsrcs:
	@echo "SRCS:        " $(SRCS) $(seperator)
	@echo "OBJS:        " $(OBJS) $(seperator)
	@echo "INTLIBS inc: " $(INTLIBS_INCLUDE) $(seperator)
	@echo "INCLUDEPATH: " $(INCLUDE_PATH)  $(seperator)
	@echo "LIBPATH:     " $(LIB_PATH) $(seperator)
	@echo "INTLIBS_TARGETS: " $(INTLIBS_TARGETS) $(seperator)

showlibs:
	@echo $(INTLIBS_TARGETS)


ifneq ($(MAKECMDGOALS),showsrcs)
  ifneq ($(MAKECMDGOALS),clean)
    ifneq ($(MAKECMDGOALS),clean.d)
      ifneq ($(MAKECMDGOALS),showlibs)
        ifneq ($(MAKECMDGOALS),lists)
          ifneq ($(MAKECMDGOALS),veryclean)
            -include $(DEPS)
          endif
        endif
      endif
    endif
  endif
endif
